let moduleExports = {};

moduleExports.pathToName = function (path) {
    return path.replace(new RegExp( '\\\\' , "g" ), "_").replace(new RegExp( '\\\\' , "g" ), "_").replace(new RegExp( '\/' , "g" ), "_");

    // return path.replaceAll('\\\\', "_").replaceAll('\\', "_").replaceAll('\/', "_");
};

moduleExports.app = function () {
    return window.app;
};

moduleExports.array_intersect = function (...args) {
    if (!args) return [];
    if (args.length == 0) return [];

    let result = args[0];

    for (let i = 1; i < args.length; i++) {
        result = result.filter(function (num) {
            return args[i].indexOf(num) !== -1;
        });
        if (result.length == 0) break;
    }

    return result;
};

moduleExports.protoIsYTClass = function (value) {
    if (!value) return false;
    if (typeof value != 'function') return false;
    let max = 10;
    let i = 0;
    while (value && (i < max)) {
        if (value.getClassType) return true;
        value = value.prototype;
        i++;
    }
    return false;
};

moduleExports.objectIsYTClass = function (value) {
    if (!value) return false;
    if (typeof value != 'object') return false;
    return value.getClassType ? true : false;
};

function randString(len) {
    len = len || 32;
    let $chars = 'abcdefghijklmnopqrstuvwxyz';
    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/

    let maxPos = $chars.length;
    let pwd = '';
    for (let i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}

moduleExports.randString = randString;

moduleExports.inArray = function (search, array, isint = false) {
    for (let i in array) {
        let temp = isint ? this.S2I(array[i]) : array[i];
        if (temp == search) {
            return true;
        }
    }
    return false;
}


moduleExports.createID = function (head) {
    return (head || '') + (new Date()).getTime() + randString(8);
};

moduleExports.getWinHeight = function (){
    try {
        let winHeight = 0;
        if (window.innerHeight)
            winHeight = window.innerHeight;
        else if ((document.body) && (document.body.clientHeight))
            winHeight = document.body.clientHeight;
        //通过深入Document内部对body进行检测，获取浏览器窗口高度
        if (document.documentElement && document.documentElement.clientHeight)
            winHeight = document.documentElement.clientHeight;
        return winHeight;
    }
    catch (e) {

    }

}


let WebStorageCache = require('../vendor/web-storage-cache.min');

function getCache() {
    if (!window.ytCache) {
        window.ytCache = new WebStorageCache();
    }
    return window.ytCache;
}

moduleExports.setCookie = function(key, value, options) {
    getCache().deleteAllExpires();
    getCache().set(key, value, options);
}

moduleExports.getCookie = function(key) {
    return getCache().get(key);
}

moduleExports.S2I = function(value) {
    return parseInt(value) || 0;
}

moduleExports.getHrefPath = function(url) {
    let arr = url.split("/");
    if (arr[arr.length-1].lastIndexOf('.') >= 0) delete arr[arr.length-1];
    let result = arr.join("/");
    if (result[result.length - 1] != '/') result = result + '/';
    return result;
}

moduleExports.getUrlParams = function() {
    let param, url = location.search, theRequest = {};
    if (url.indexOf("?") != -1) {
        let str = url.substr(1);
        let strs = str.split("&");
        for (let i = 0, len = strs.length; i < len; i++) {
            param = strs[i].split("=");
            theRequest[param[0]] = decodeURIComponent(param[1]);
        }
    }
    return theRequest;
}

moduleExports.getWidth = function() {
    return document.documentElement.clientWidth;
}

moduleExports.getHeight = function() {
    return document.documentElement.clientHeight;
}


module.exports = moduleExports;
